"""
Case Type   : 审计相关参数
Case Name   : 修改audit_data_format、audit_directory参数超出长度限制合理报错
Create At   : 2023/10/9
Owner       : ningyali
Description :
    1.通过guc set设置参数audit_data_format、audit_directory参数超出长度限制
    2.通过alter system set设置参数audit_data_format、audit_directory参数超出长度限制
Expect      :
    1.设置失败，合理报错
    2.设置失败，合理报错
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class GucAudit1(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.param = ('audit_data_format', 'audit_directory')
        self.param_value = '10'*1000

    def test_1(self):
        text = f'----step1: 通过guc set设置参数{",".join(self.param)}参数超出长度限制' \
            f' expect: 设置失败，合理报错----'
        self.log.info(text)
        for param in self.param:
            res = self.pri_sh.execute_gsguc('set', '',
                                            f'{param}={self.param_value}',
                                            get_detail=True)
            expect = f'The value of parameter {param} is too long'
            self.assertIn(expect, res, f'执行失败：{text}')

        text = f'----step2: 通过alter system set设置参数{",".join(self.param)}' \
            f'参数超出长度限制 expect: 设置失败，合理报错----'
        self.log.info(text)
        expect = 'ERROR:  the length of GUC string type value exceed 1024'
        for param in self.param:
            sql = f'alter system set {param}={self.param_value}'
            res = self.pri_sh.execut_db_sql(sql)
            self.assertIn(expect, res, f'执行失败：{text}')

    def tearDown(self):
        text = '----run teardown----'
        self.log.info(text)
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
